package com.ruoyi.project.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mall.domain.MallNewsClassDO;
import com.ruoyi.project.mall.service.MallNewsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 咨询分类Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/mall/mallNewsClass")
public class MallNewsClassController extends BaseController
{
	@Autowired
	private MallNewsClassService mallNewsClassService;

	/**
	 * 查询咨询分类列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:list')")
	@GetMapping("/list")
	public TableDataInfo list(MallNewsClassDO mallNewsClass)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallNewsClass);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","ca_at");
		stringObjectMap.put("order","desc");
		List<MallNewsClassDO> list = mallNewsClassService.likeList(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出咨询分类列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:export')")
	@Log(title = "咨询分类", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MallNewsClassDO mallNewsClass)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallNewsClass);
		List<MallNewsClassDO> list = mallNewsClassService.list(stringObjectMap);
		ExcelUtil<MallNewsClassDO> util = new ExcelUtil<MallNewsClassDO>(MallNewsClassDO.class);
		return util.exportExcel(list, "mallNewsClass");
	}

	/**
	 * 获取咨询分类详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(mallNewsClassService.get(id));
	}

	/**
	 * 新增咨询分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:add')")
	@Log(title = "咨询分类", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallNewsClassDO mallNewsClass)
	{
		mallNewsClass.setId(IdUtil.simpleUUID());
		mallNewsClass.setDelFlag(0);
		mallNewsClass.setCaAt(new Date());
		return toAjax(mallNewsClassService.save(mallNewsClass));
	}

	/**
	 * 修改咨询分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:edit')")
	@Log(title = "咨询分类", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallNewsClassDO mallNewsClass)
	{
		mallNewsClass.setUpAt(new Date());
		return toAjax(mallNewsClassService.update(mallNewsClass));
	}

	/**
	 * 删除咨询分类
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallNewsClass:remove')")
	@Log(title = "咨询分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				MallNewsClassDO mallNewsClass = mallNewsClassService.get(string);
				mallNewsClass.setDelFlag(1);
				mallNewsClass.setUpAt(new Date());
				mallNewsClassService.update(mallNewsClass);
			}
		}
		return toAjax(ids.length);
		//return toAjax(mallNewsClassService.deleteMallNewsClassByIds(ids));
	}
}
